package com.wanmait.baohan.controller.CustomerController;

import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.*;

import com.wanmait.baohan.vo.DataVo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

//企业需求liu
@Controller
@RequestMapping("customerInformation")
public class DemandController {
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private DemandService demandService;
    @Resource
    private AgentService agentService;
    @Resource
    private AccountService accountService;
    @Resource
    private SuppilerService suppilerService;
    @Resource
    private BusinessContactService businessContactService;
    @GetMapping("addDemandByCustomer")
    public ModelAndView addDemandByCustomer()
    {
        ModelAndView modelAndView = new ModelAndView();

        List<Map<String,Object>> demandTypeAMaps = dictionaryService.getDemandTypeA();
        modelAndView.addObject("demandTypeAMaps",demandTypeAMaps);

        List<Map<String,Object>> demandTypeBMaps = dictionaryService.getDemandTypeB();
        modelAndView.addObject("demandTypeBMaps",demandTypeBMaps);

        List<Map<String,Object>> demandTypeCMaps = dictionaryService.getDemandTypeC();
        modelAndView.addObject("demandTypeCMaps",demandTypeCMaps);

        List<Map<String,Object>> demandTypeDMaps = dictionaryService.getDemandTypeD();
        modelAndView.addObject("demandTypeDMaps",demandTypeDMaps);

        List<Map<String,Object>> demandTypeEMaps = dictionaryService.getDemandTypeE();
        modelAndView.addObject("demandTypeEMaps",demandTypeEMaps);

        //modelAndView.addObject("customerId",supplierId);
        Supplier supplier = new Supplier();
        List<Supplier> suppliers = suppilerService.selectAllot(supplier);
        modelAndView.addObject("suppliers",suppliers);
        return modelAndView;
    }
    @PostMapping("addDemandByCustomer")
    public String addDemandByCustomer(Demand demand)
    {
        System.out.println(demand.getDictionaryA().getId());
        System.out.println(demand.getDictionaryB().getId());
        System.out.println(demand.getModel());
        System.out.println(demand.getSupplier().getId());
        demandService.insert(demand);
        return "redirect:/customerInformation/demandsList";
    }
    @RequestMapping("demandsDetailed")
    public ModelAndView demandsDetailed(String demandId,String customerId)
    {
        ModelAndView modelAndView = new ModelAndView();
        Demand demand = demandService.findOneBy(Integer.parseInt(demandId));
        modelAndView.addObject("demand",demand);
        System.out.println(customerId);
        modelAndView.addObject("customerId",customerId);
        return modelAndView;
    }
    @RequestMapping("/demandsList")
    public void demandsData()
    {

    }
    @RequestMapping("demandsData")
    @ResponseBody
    public DataVo<Demand> demandsData(String customerName,String demandName)
    {
        Demand demand = new Demand();
        if(StringUtils.hasText(demandName))
        {
            Dictionary dictionary = new Dictionary();
            dictionary.setValue(demandName);
            demand.setDictionaryB(dictionary);
            //System.out.println(productName);
        }
        if(StringUtils.hasText(customerName))
        {
            Supplier supplier = new Supplier();
            supplier.setName(customerName);
            demand.setSupplier(supplier);
            //System.out.println(customerName);
        }
        List<Demand> demands = demandService.findAllBy(demand);
        DataVo<Demand> dataVo = new DataVo<>();
        dataVo.setCount((long) demands.size());
        dataVo.setData(demands);
        return dataVo;
    }

    //ajax转发的数据
    @RequestMapping("dataLista")
    @ResponseBody
    public Account dataLista(String accountId)
    {
        System.out.println(accountId);
        Account account = accountService.findById(Integer.parseInt(accountId));
        return account;
    }
    @RequestMapping("dataList")
    @ResponseBody
    public BusinessContact dataList(String businessContactId)
    {
        System.out.println(businessContactId);
        BusinessContact businessContact = businessContactService.findById(Integer.parseInt(businessContactId));
        return businessContact;
    }
    //签约信息填写
    @GetMapping("addSigningInformation")
    public ModelAndView addSigningInformation(String supplierId,String supplierName)
    {
        System.out.println(supplierName);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("customerId",supplierId);
        Supplier supplier = suppilerService.findSupplierByName(supplierName);

        modelAndView.addObject("supplier",supplier);
        return modelAndView;
    }
    @PostMapping("addSigningInformation")
    public void addSigningInformation(Contract contract)
    {
        System.out.println(contract.getRemarks());
        System.out.println(contract.getAgent().getId());
        System.out.println(contract.getSupplier().getId());

    }
    @GetMapping("addAcountInformation")
    public ModelAndView addAcountInformation(String supplierId)
    {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> dictionaryListA = dictionaryService.getAcountTypeA();
        List<Map<String, Object>> dictionaryListB = dictionaryService.getAcountTypeB();
        List<Map<String, Object>> dictionaryListC = dictionaryService.getAcountTypeC();
        modelAndView.addObject("dictionaryListA",dictionaryListA);
        modelAndView.addObject("dictionaryListB",dictionaryListB);
        modelAndView.addObject("dictionaryListC",dictionaryListC);
        modelAndView.addObject("customerId",supplierId);
        return modelAndView;
    }
    @PostMapping("addAcountInformation")
    public void addAcountInformation(Account account,MultipartFile file)
    {
        account.setPermit(file.getOriginalFilename());
        //System.out.println(account.getBankName());
        accountService.insert(account);
    }

    //代理商添加.
    @GetMapping("addAgents")
    public ModelAndView addAgents(String supplierId)
    {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> dictionaryList = dictionaryService.getAgentType();
        modelAndView.addObject("dictionaryList",dictionaryList);
        modelAndView.addObject("customerId",supplierId);
        return modelAndView;
    }
    @PostMapping("addAgents")
    public void addAgents(Agent agent, MultipartFile file)
    {
        agent.setPowerAttorney(file.getOriginalFilename());
       /* System.out.println(agent.getRemarks());
        System.out.println(file.getOriginalFilename());
        System.out.println(agent.getName());
        System.out.println(agent.getDictionary().getId());*/

        agentService.insert(agent);
    }
    @GetMapping("/addDemand")
    public ModelAndView addDemand(String supplierId)
    {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String,Object>> demandTypeAMaps = dictionaryService.getDemandTypeA();
        modelAndView.addObject("demandTypeAMaps",demandTypeAMaps);

        List<Map<String,Object>> demandTypeBMaps = dictionaryService.getDemandTypeB();
        modelAndView.addObject("demandTypeBMaps",demandTypeBMaps);

        List<Map<String,Object>> demandTypeCMaps = dictionaryService.getDemandTypeC();
        modelAndView.addObject("demandTypeCMaps",demandTypeCMaps);

        List<Map<String,Object>> demandTypeDMaps = dictionaryService.getDemandTypeD();
        modelAndView.addObject("demandTypeDMaps",demandTypeDMaps);

        List<Map<String,Object>> demandTypeEMaps = dictionaryService.getDemandTypeE();
        modelAndView.addObject("demandTypeEMaps",demandTypeEMaps);

        modelAndView.addObject("customerId",supplierId);
        //System.out.println(supplierId);
        return modelAndView;
    }
   @PostMapping("addDemand")
    public void addDemand(Demand demand)
    {
        System.out.println(demand.getDictionaryA().getId());
        System.out.println(demand.getDictionaryB().getId());
        System.out.println(demand.getModel());
        demandService.insert(demand);

    }
}
